import http from '../http.js'

function handleResponse(url, res, callback) {
  let {
    data
  } = res;
  if (data.success) {
    callback(data);
  } else {
    // console.warn("接口返回不成功", url, res)
    // wx.showToast({
    //   title: data.message,
    //   icon: "none"
    // })
  }
}
export const getCarTravel = (data, callback) => {
  let url = '/jeecg-boot/logi/logiShipmentInfo/getCarTravel'
  http({
    url: url,
    method: "GET",
    data: {
      startTime: data.startTime,
      endTime: data.endTime,
      plateNumber: data.plateNumber
    }
  }).then(res => {
    handleResponse(url, res, callback)
  })
}
/**
 * 验证码
 * @param {String} captchaKey 
 * @param {*} callback 
 */
export const fetchCaptchaApi = (captchaKey, callback) => {
  let url = `/jeecg-boot/sys/randomImage/${captchaKey}`
  http({
    url: url,
    method: "GET",
    header: {}
  }).then(res => {
    callback(res.data);
  });
}
/**
 * 登录接口
 * @param {Object} data {captcha, checkKey, password, username}
 * @param {*} callback 
 */
export const loginApi = (data, callback) => {
  let url = '/jeecg-boot/sys/appletLogin'
  http({
    url: url,
    method: "POST",
    data: data,
    header: {}
  }).then(res => {
    let {
      data
    } = res;
    if (data.success) {
      wx.setStorageSync('token', data.result.token)
      wx.setStorageSync('userInfo', data.result.userInfo)
      wx.setStorageSync('logiRole', data.result.logiRole)
    }
    callback(data);
  })
}

/**
 * 上传图片(同步)
 * @param {*} data {}
 * @param {*} callback 
 */
export const uploadImage = async (data, callback) => {
  let url = '/jeecg-boot/logi/logiShipmentInfo/uploadImage'
  let res = await http({
    url: url,
    method: "POST",
    data: {
      imgData: data.imgData
    }
  })
  handleResponse(url, res, callback)
}
/**
 * 发货表-分页列表查询
 * @param {*} data {}
 * @param {*} callback 
 */
export const listShipmentInfoApi = (data, callback) => {
  let url = '/jeecg-boot/logi/logiShipmentInfo/appletList'
  http({
    url: url,
    method: "GET",
    data: {
      days: data.days,
      rowState: data.rowstate
    }
  }).then(res => {
    handleResponse(url, res, callback)
  })
}
/**
 * 发货表-通过id查询
 * @param {*} data 
 * @param {*} callback 
 */
export const getShipmentByIdApi = (data, callback) => {
  let url = '/jeecg-boot/logi/logiShipmentInfo/queryById'
  http({
    url: url,
    method: "GET",
    data: {
      id: data.id
    }
  }).then(res => {
    handleResponse(url, res, callback)
  })
}
/**
 * 小程序发货单接收接口。上送接收标志（确认或拒绝），更改发货单状态后，获取新的发货单信息。
 * shipmentId 发货单号，List接口获取发货单列表中有
 * recvFlag为：0-接收确认，1-拒绝，拒绝时需要填写理由，填在reason中。
 * @param {*} data 
 * @param {*} callback 
 */
export const receptionShipment = (data, callback) => {
  let url = '/jeecg-boot/logi/logiShipmentInfo/appletReception'
  http({
    url: url,
    method: "GET",
    data: {
      reason: data.reason,
      recvFlag: data.recvFlag,
      shipmentId: data.shipmentId
    }
  }).then(res => {
    handleResponse(url, res, callback)
  })
}
/**
 * 发货表-上传配送中止信息及图片
 * @param {*} data 
 * @param {*} callback 
 */
export const abortShipment = (data, callback) => {
  let url = '/jeecg-boot/logi/logiShipmentInfo/abortShipmentInfo'
  http({
    url: url,
    method: "GET",
    data: {
      abortDate: data.abortDate,
      abortReason: data.abortReason,
      fileNames: data.fileNames,
      shipmentId: data.shipmentId
    }
  }).then(res => {
    handleResponse(url, res, callback)
  })
}
/**
 * 发货表-上传实际发货的信息及图片
 * @param {*} data 
 * @param {*} callback 
 */
export const actualShipment = (data, callback) => {
  let url = '/jeecg-boot/logi/logiShipmentInfo/actualShipmentInfo'
  http({
    url: url,
    method: "GET",
    data: {
      fileNames: data.fileNames,
      shipDate: data.shipDate,
      shipmentId: data.shipmentId,
      remark: data.remark
    }
  }).then(res => {
    handleResponse(url, res, callback)
  })
}
/**
 * 发货表-上传签收完成的信息及图片文件名
 * @param {*} data 
 * @param {*} callback 
 */
export const completeShipment = (data, callback) => {
  let url = '/jeecg-boot/logi/logiShipmentInfo/completeShipmentInfo'
  http({
    url: url,
    method: "GET",
    data: {
      fileNames: data.fileNames,
      completeDate: data.completeDate,
      shipmentId: data.shipmentId,
      remark: data.remark
    }
  }).then(res => {
    handleResponse(url, res, callback)
  })
}
/**
 * 发货明细行-通主表shipmentId查询。查取高发货单下的所有明细行信息。
 * @param {*} data 
 * @param {*} callback 
 */
export const queryLineByShipmentId = (data, callback) => {
  let url = '/jeecg-boot/logi/logiShipmentInfo/queryLineByShipmentId'
  http({
    url: url,
    method: "GET",
    data: {
      shipmentId: data.shipmentId
    }
  }).then(res => {
    handleResponse(url, res, callback)
  })
}

/**
 * 修改密码。
 */
export const updatePassword = (data, callback) => {
  let url = '/jeecg-boot/sys/user/updatePassword'
  http({
    url: url,
    method: "PUT",
    data: {
      confirmpassword: data.confirmpassword,
      oldpassword: data.oldpassword,
      password: data.password,
      username: data.username
    }
  }).then(res => {
     callback(res.data)
  })
}